package com.xjoyt.pay.master.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import com.xjoyt.pay.abstracts.AbstractService;
import com.xjoyt.pay.commons.ObjectUtils;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.master.params.DistributorProfitGroupParam;
import com.xjoyt.pay.master.params.DistributorProfitPageParam;
import com.xjoyt.pay.master.po.Distributor;
import com.xjoyt.pay.master.po.DistributorProfit;
import com.xjoyt.pay.master.repository.DistributorProfitRepository;
import com.xjoyt.pay.master.repository.DistributorRepository;
import com.xjoyt.pay.master.vo.DistributorProfitGroup;
import com.xjoyt.pay.merchant.register.params.DistributorPageParam;

@Service
public class DistributorProfitService extends AbstractService {

	@Autowired
	private DistributorProfitRepository repository;

	@Autowired
	private DistributorRepository distributorRepository;

	public PageResult<DistributorProfitGroup> loadPageGroup(@RequestBody PageParam<DistributorProfitGroupParam> param) {
		PageParam<DistributorPageParam> p = new PageParam<>();
		ObjectUtils.fieldCopy(param, p);

		PageResult<Distributor> page = distributorRepository.loadPage(p);

		List<String> distributorIds = new ArrayList<>();
		List<DistributorProfitGroup> list = new ArrayList<>();
		for (Distributor item : page) {
			distributorIds.add(item.id);
			DistributorProfitGroup vo = new DistributorProfitGroup();
			list.add(vo);
			ObjectUtils.fieldCopy(item, vo);
		}

		List<Object> footer = new ArrayList<>();

		if (CollectionUtils.isNotEmpty(distributorIds) && param.query != null || param.query.canCheck) {
			
			List<Map<String, Object>> sumList = repository.sum(distributorIds, param.query);

			double moneySum = 0;
			double profitSum = 0;
			for (Map<String, Object> map : sumList) {
				if (map == null || !map.containsKey("id") || map.get("id") == null)
					continue;
				Object distributorId = map.get("id");
				for (DistributorProfitGroup item : list) {
					if (distributorId.equals(item.id)) {
						item.money = (map.containsKey("moneySum") && map.get("moneySum") != null)
								? Double.valueOf(map.get("moneySum").toString()) : 0D;
						item.profit = (map.containsKey("profitSum") && map.get("profitSum") != null)
								? Double.valueOf(map.get("profitSum").toString()) : 0L;
						moneySum += item.money;
						profitSum += item.profit;
						break;
					}
				}
			}

			Map<String, Double> map = new HashMap<>();
			map.put("moneySum", moneySum);
			map.put("profitSum", profitSum);
			footer.add(map);
		}

		return new PageResult<>(page.getTotal(), list, footer);
	}

	public PageResult<com.xjoyt.pay.master.vo.DistributorProfit> loadPageMap(
			@RequestBody PageParam<DistributorProfitPageParam> param) {

		PageResult<DistributorProfit> page = repository.loadPage(param);
		List<com.xjoyt.pay.master.vo.DistributorProfit> list = new ArrayList<>();
		List<String> distributorIds = new ArrayList<>();
		for (DistributorProfit item : page) {
			distributorIds.add(item.distributor.id);
			com.xjoyt.pay.master.vo.DistributorProfit vo = new com.xjoyt.pay.master.vo.DistributorProfit();
			list.add(vo);
			ObjectUtils.fieldCopy(item, vo);
		}

		List<Object> footer = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(distributorIds) && param.query != null) {
			List<Map<String, Object>> sumList = repository.profitSum(distributorIds, param.query);
			if (CollectionUtils.isNotEmpty(sumList)) {
				for (Map<String, Object> map : sumList) {
					if (map == null || !map.containsKey("id") || map.get("id") == null) {
						continue;
					}
					Map<String, Object> map1 = new HashMap<>();
					map1.put("distributorId", (map.containsKey("id") && map.get("id") != null) ? map.get("id") : 0);
					map1.put("moneySum",
							(map.containsKey("moneySum") && map.get("moneySum") != null) ? map.get("moneySum") : 0);
					map1.put("profitSum",
							(map.containsKey("profitSum") && map.get("profitSum") != null) ? map.get("profitSum") : 0);
					footer.add(map1);
				}
			}
		}

		return new PageResult<>(page.getTotal(), list, footer);
	}

}
